import { store } from "@/store";
import { isNil } from "lodash-es";
import Action from "../extra/action";
import { Battle } from "./Battle";
import { Character, CharacterData } from "./Character";
import { Player } from "@/extra/player";
import { skillTable } from "@/data/SkillTable";
export default class ActionManager {
    __action: Action[] = []
    __localaction: any[] = []
    events: (Function | null)[] = []
    actnum: number
    length = 0
    battle: Battle
    lastaction?: Action
    isPlaying = false
    pending: Action[] = []
    constructor(battle: Battle) {
        this.battle = battle
        this.actnum = battle.actnum
    }
    addAction(action: Action) {
        this.__action[action.index] = action
        this.length = Math.max(this.length, action.index)
    }
    async playAction(index?: number) {
        let args = this.nextAction
        while (args && store.videoPlaying) {
            this.isPlaying = true
            if (index && this.actnum + 1 > index) {
                break
            }
            await this[args.name](args)
            for (let i in args.objs) {
                this.getfightcard(args.objs[i])
            }
            await this.battle.refreshAuras()
            this.actnum = this.actnum + 1
            args = this.__action[this.actnum + 1] || this.pending.shift()
        }
        this.isPlaying = false
    }
    getfightcard(card: CharacterData, creator?: Player) {
        let fightcard: Character | undefined
        if (this.battle.characters[card.id]) {
            fightcard = this.battle.characters[card.id]
            if (fightcard) {
                if (card.character_id !== 0) {
                    fightcard.init(card)
                    fightcard.dirty = true
                }
            }

        } else if (card.character_id !== 0) {
            let player = this.getplayer(card.player)
            fightcard = this.battle.createCharacter(player, creator, card)
            fightcard.dirty = true
            return fightcard
        }

        return fightcard
    }
    get nextAction() {
        return this.__action[this.actnum + 1]
    }
    delAction() {
        if (this.__localaction[1]) {
            this.onDataEvent(this.__localaction[1])
            this.__localaction.splice(1, 1)
        } else {
            this.onDataEvent(this.__action[this.actnum + 1])
            this.actnum = this.actnum + 1
            this.lastaction = this.__action[this.actnum]
        }
    }
    onDataEvent(data) {
        this.events.forEach(callback => {
            callback?.(data)
        })
    }
    regEventCallback(tag, callback: Function) {
        this.events[tag] = callback
    }
    delEventCallback(tag) {
        this.events[tag] = null
    }
    getplayer(id: number) {
        if (this.battle.playerA.id === id) {
            return this.battle.playerA
        } else {
            return this.battle.playerB
        }
    }

    begin(args) {
        // 回合开始
        this.battle.round()
    }
    castSkill(args: Action) {
        const minion = this.battle.characters[args.obj?.id]
        const enemy = this.battle.characters[args.sub?.id]
    }
    attack(args: Action) {
        const minion = this.battle.characters[args.obj?.id]
        const enemy = this.battle.characters[args.sub?.id]
        if (minion && args.para?.damge) {
            minion.currentHP -= args.para.damge
            console.log(`${enemy?.name} 攻击 ${minion.name} 造成 ${args.para.damge} 点伤害 血量 ${minion.temppro.currentHP}/${minion.temppro.maxHP}`);
        }
    }
    addHP(args: Action) {
        // 预处理
        let minion = this.getfightcard(args.obj)
        // 逻辑处理
        if (minion) {
            minion.currentHP = args.obj.currentHP
        }
    }
}